import 'package:flutter/material.dart';
import 'package:package_info/package_info.dart';
import 'package:wanAndroid/common/uts/ui_uts.dart';
import 'package:wanAndroid/page/page_webview.dart';
import 'package:wanAndroid/res/colors.dart';

class PageAbout extends StatefulWidget {
  ///启动页
  static Future<void> start(BuildContext context) async {
    return await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => PageAbout()),
    );
  }

  @override
  State<StatefulWidget> createState() => _PageAbout();
}

class _PageAbout extends State<PageAbout> {
  final thirdSDK = [
    {
      "name": "Dio",
      "desc":
          "dio是一个强大的Dart Http请求库，支持Restful API、FormData、拦截器、请求取消、Cookie管理、文件上传/下载、超时、自定义适配器等...",
      "url": "https://github.com/flutterchina/dio"
    },
    {
      "name": "pull_to_refresh",
      "desc": "一个提供上拉加载和下拉刷新的组件,同时支持Android和Ios",
      "url": "https://github.com/peng8350/flutter_pulltorefresh"
    },
    {
      "name": "webview_flutter",
      "desc": '''
      提供WebView小部件的flatter插件。在iOS上，WebView小部件由WKWebView支持；在Android上，WebView小部件由WebView支持。
      ''',
      "url":
          "https://github.com/flutter/plugins/tree/master/packages/webview_flutter"
    },
    {
      "name": "shared_preferences",
      "desc": '''
      包装特定于平台的简单数据持久存储（iOS和macOS上的NSUserDefaults、Android上的SharedReference等）。数据可能以异步方式持久化到磁盘，并且不能保证在返回后写入将持久化到磁盘，因此此插件不能用于存储关键数据。
      ''',
      "url": "https://github.com/flutter/plugins"
    },
    {
      "name": "cookie_jar/dio_cookie_manager",
      "desc": "Coojkie管理",
      "url": "https://github.com/flutterchina/dio"
    },
    {
      "name": "package_info",
      "desc": "获取FlutterApp版版本信息",
      "url": "https://github.com/flutter/plugins"
    },
    {
      "name": "FlutterToast",
      "desc": "现在这个toast库支持两种toast消息，一种需要BuildContext，另一种不需要BuildContext",
      "url": "https://github.com/PonnamKarthik/FlutterToast"
    }
  ];

  ///app 版本信息
  PackageInfo _packageInfo = PackageInfo(
    appName: 'Unknown',
    packageName: 'Unknown',
    version: 'Unknown',
    buildNumber: 'Unknown',
  );

  @override
  void initState() {
    getAppInfo();
    super.initState();
  }

  //获取版本信息
  Future<void> getAppInfo() async {
    final PackageInfo info = await PackageInfo.fromPlatform();
    setState(() {
      _packageInfo = info;
    });
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        backgroundColor: ResColors.scaffoldColor,
        appBar: AppBar(
          title: UiUts.getTitle("关于"),
        ),
        body: ListView(
          children: [AppVer()]..addAll(getInfoViews()),
        ),
      );

  ///版本信息
  ///
  Widget AppVer() => Container(
        width: double.infinity,
        constraints: BoxConstraints(
          minHeight: 200,
          maxHeight: 200,
          minWidth: double.infinity,
        ),
        alignment: Alignment.center,
        padding: const EdgeInsets.only(
          top: 50,
          bottom: 20,
        ),
        decoration: BoxDecoration(
          color: Colors.white,
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            UiUts.getText(
              this._packageInfo.appName,
              fontSize: 30,
            ),
            UiUts.getDivider(
              height: 20,
            ),
            UiUts.getText(
              this._packageInfo.packageName,
              fontSize: 18,
            ),
            UiUts.getDivider(
              height: 5,
            ),
            UiUts.getText(
              "版本：${this._packageInfo.version}  版本号：${this._packageInfo.buildNumber}",
              fontSize: 18,
            ),
          ],
        ),
      );

  ///使用的第三方框架
  ///
  List<Widget> getInfoViews() => thirdSDK
      .map(
        (e) => InkWell(
          child: Container(
            margin: const EdgeInsets.only(
              top: 5,
            ),
            padding: const EdgeInsets.symmetric(
              vertical: 10,
            ),
            decoration: BoxDecoration(
              color: Colors.white,
            ),
            child: ListTile(
              trailing: const Icon(
                Icons.keyboard_arrow_right,
                size: 30,
                color: ResColors.primaryColor,
              ),
              title: UiUts.getTitle(
                e["name"],
                color: ResColors.fontColor,
                fontSize: 18,
              ),
              subtitle: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  UiUts.getDivider(
                    height: 5,
                  ),
                  UiUts.getText("${e["desc"]}",
                      fontSize: 14, color: ResColors.fontColor),
                  UiUts.getDivider(
                    height: 5,
                  ),
                  UiUts.getText("${e["url"]}",
                      fontSize: 14, color: ResColors.fontColor),
                ],
              ),
            ),
          ),
          onTap: () => {
            //点击查看
            PageWebView.start(context, "${e["url"]}", title: "${e["desc"]}")
          },
        ),
      )
      .toList();
}
